//获取应用实例
const app = getApp();
Page({
	data: {
		year:0,
		month:0,
		date:['日', '一', '二', '三', '四', '五', '六'],
		dateArr:[],
		isToday:0,
		isTodayWeek:false,
		todayIndex:0,
		CumulativeSign:1,
		continuitySign:1,
		signList:[],
		isShow:false
	},
	//关闭弹窗
	claseAlert:function(){
		var that = this;
		that.setData({
			isShow:false
		})
		that.onLoad();
	},
	onLoad:function(){
		var that  = this;
		let now   = new Date();
		let year  = now.getFullYear();
		let month = now.getMonth() + 1;
		if(month < 10) {
			month = '0' + month
		}
		this.setData({
			year:year,
			month:month,
			isToday:year + '-' + month + '-' + now.getDate()
		})		
		that.dateInit();
		//查询签到
		app.request('/api/Member/signinList', {}, function(res){
			if(res.code == '0'){
				that.setData({
					CumulativeSign:res.data.cumulative,
					continuitySign:res.data.continuity,
					signList:res.data.list
				})
				var _signDate = that.data.dateArr;
				for(var i=0; i<that.data.dateArr.length; i++){
					for(var j = 0; j < that.data.signList.length; j++) {
						if(that.data.dateArr[i].isToday == that.data.signList[j]){							
							_signDate[i].sign = true;
						}
					}
				}					
				that.setData({
					dateArr: _signDate
				})
			}
		});
	},
	dateInit:function(setYear, setMonth){
		var that = this;
		//全部时间的月份都是按0~11基准，显示月份才+1
		let dateArr = []; //需要遍历的日历数组数据
		let arrLen = 0; //dateArr的数组长度
		let now = setYear ? new Date(setYear, setMonth) : new Date();
		let year = setYear || now.getFullYear();
		let nextYear = 0;
		let month = setMonth || now.getMonth(); //没有+1方便后面计算当月总天数		
		let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
//		let startWeek = new Date(year + ',' + (month + 1) + ',' + 1).getDay();
        let startMonth = new Date()
        startMonth.setDate(1);
		let startWeek = startMonth.getDay(); //目标月1号对应的星期
		let dayNums = new Date(year, nextMonth, 0).getDate(); //获取目标月有多少天
		let obj = {};
		let num = 0;
		if(month + 1 > 11) {
			nextYear = year + 1;
			dayNums = new Date(nextYear, nextMonth, 0).getDate();
		}
		arrLen = startWeek + dayNums;
		for(let i = 0; i < arrLen; i++) {
			if(i >= startWeek) {
				num = i - startWeek + 1;
				if(month < 9) {
					var _month = '0' + (month + 1)
				}
				else {
					var _month = parseInt(month) + 1
				}				
				if(num < 9) {
//					var _num = '0' + (num + 1)					
					var _num = '0' + num
				} else if(num == 9){
					var _num = '0' + num
				}else {
					_num = parseInt(_num) + 1
					
				}
				obj = {
					isToday: '' + year + '-' + _month + '-' + _num,
					dateNum: num,
					weight: 5
				}				
				
			} else {
				obj = {};
			}
			dateArr[i] = obj;
		}		
		that.setData({
			dateArr: dateArr
		})
		let nowDate = new Date();
		let nowYear = nowDate.getFullYear();
		let nowMonth = nowDate.getMonth() + 1;
		let nowWeek = nowDate.getDay();
		let getYear = setYear || nowYear;
		let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;
        console.log(nowDate,'11',nowYear,'222',nowMonth,'333',nowWeek,'444',getYear,'555',getMonth)
        console.log(that.data.date[nowWeek])
		if(nowYear == getYear && nowMonth == getMonth) {
			this.setData({
				isTodayWeek: true,
				todayIndex: nowWeek
			})
		} else {
			this.setData({
				isTodayWeek: false,
				todayIndex: -1
			})
		}
	},
	lastMonth: function() {
		//全部时间的月份都是按0~11基准，显示月份才+1
		let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
		let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
		this.setData({
			year: year,
			month: (month + 1)
		})
		this.dateInit(year, month);
	},
	nextMonth: function() {
		//全部时间的月份都是按0~11基准，显示月份才+1
		let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
		let month = this.data.month > 11 ? 0 : this.data.month;
		this.setData({
			year: year,
			month: (month + 1)
		})
		this.dateInit(year, month);
	},
	formSubmit:function(e){
		var that = this;
		app.request('/api/Member/dailySignin', {}, function(res){
			if(res.code == '0'){
				//更新数据
				that.setData({
					isShow:true
				});
			}else{
				wx.showToast({
					title:res.msg,
					icon:'none'
				})
			}
		});
	}
})